<?php

class IndexController extends Controller{
	private $DB;
	public function __Init(){
		$this->DB = new IndexModel;
		// 获得分类
		$CatName = $this->DB->getCategory();
		$this->assign('CatName',$CatName);
		// 最新的博文
		$NewBlog = $this->DB->getNew();
		$this->assign('New',$NewBlog);
		// 点击排行榜
		$Click = $this->DB->Click();
		// p($Click);
		$this->assign('Click',$Click);
		// 获取点击量最多的5条博文
		$Now = $this->DB->order(" click DESC ")->limit(5)->select();
		$this->assign('Nows',$Now);
	}
	// 首页面
	public function index(){
		// 首页面默认列出来10条
		$data = $this->DB->order("bid DESC")->limit(10)->getAllContents();
		
		$this->assign('Content',$data);

		$this->display('index.html');
	}
	// 内容详情页
	public function View(){
		// 当前文章
		$data = $this->DB->where("bid = {$_GET['bid']}")->find();
		// 上一篇文章
		$dataPre = $this->DB->where("bid < {$data['bid']}")->order('bid DESC')->limit(1)->find();
		// 下一篇文章
		$dataNext = $this->DB->where("bid >{$data['bid']}")->order('bid ASC')->limit(1)->find();
		// 获取当前文章相关的文章
		$relevance = $this->DB->where("cid = {$data['cid']}")->order("rand()")->limit("4")->select();
		// 点击排行榜，就是跟当前文章相关的分类里面进行排行
		$clicks = $this->DB->where("cid = {$data['cid']}")->order("click DESC")->limit("5")->select();
		// 获取当前文章相关的文章
		$this->assign('relevance',$relevance);
		// 当前文章
		$this->assign('Content',$data);
		// 上一篇文章
		$this->assign('pre',$dataPre);
		// 下一篇文章
		$this->assign('next',$dataNext);
		// 点击排行榜，就是跟当前文章相关的分类里面进行排行
		$this->assign('clicks',$clicks);
		// 当前分类名称
		$CatName = $this->DB->where("cid = {$data['cid']}")->getCategory();
		// 当前分类名称
		$this->assign('category',$CatName[0]['catname']);
		// 获得分类id
		$this->assign('cid',$CatName[0]['cid']);
		// 指定模板
		$this->display('news.html');
	}
	// 分类页面
	public function newsList(){	
		//获取cid
		$cid = $_GET['cid'];
		// 获取相应的博文
		$res = $this->DB->where("cid = {$cid}")->getAllBlog($cid);
		// 博客内容
		$this->assign('bloglist',$res['blog']);
		// 分类内容
		$this->assign('page',$res['page']);
		// 栏目推荐
		$CatHot = $this->DB->where("cid = {$cid}")->order("click DESC")->limit(5)->getAllBlog($cid);
		$this->assign("CatHot",$CatHot['blog']);
		// 分类名称
		$CatNames = $this->DB->where("cid = {$cid}")->getCategory();
		// 当前分类名称
		$this->assign('CurrentCategory',$CatNames[0]['catname']);
		// 指定模板
		$this->display('newslist.html');
	}

	public function ClickWrite(){
		$bid = $_GET['bid'];
		$DB = new Model('blog');
		$Sql = "UPDATE blog SET click = click+1 WHERE bid={$bid}";
		$DB->exe($Sql);
		$field =$DB->where("bid = $bid ")->find();
		echo "document.write({$field['click']})";
	}

}
		// $start = $this->field(" count(*) as c ")->where(" cid = {$cid} ")->select();
		// $page = new Page($start[0]['c'],5);

?>